package com.yx.study.leet.primary.other;

import java.util.Stack;

/**
 * @Classname IsValid
 * @Date 2023/02/12 23:40
 * @Author by yx
 * @Description : 有效的括号
 * https://leetcode.cn/leetbook/read/top-interview-questions-easy/xnbcaj/
 */
public class IsValid {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for (char c : chars) {
            switch (c) {
                case '(':
                    stack.push(')');
                    break;
                case '{':
                    stack.push('}');
                    break;
                case '[':
                    stack.push(']');
                    break;
                default:
                    if (stack.isEmpty() || stack.pop() != c) {
                        return false;
                    }
                    break;
            }
        }
        return stack.isEmpty();
    }
}
